﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YJKArch.Utils;
using YJKControls.Previewer;
using HYFamilyLibDataManager;

namespace YJKArch.ColumnModule
{
	public partial class ColumnInsert : System.Windows.Forms.Form
	{
		public bool IsArchColumn
		{
			get
			{
				return this.m_IsArchColumn;
			}
		}

		public int InsertType
		{
			get
			{
				return this.m_InsertType;
			}
		}

		public bool AutoRotate
		{
			get
			{
				return this.rdbAutoRotate.Checked;
			}
		}

		public bool AutoRotateWall
		{
			get
			{
				return this.rdbAutoRotateWall.Checked;
			}
		}

		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		public ElementId TopLevelId
		{
			get
			{
				return AssistFunc.GetLevelByName(this.m_docment, this.cmbTopLevel.SelectedItem.ToString()).Id;
			}
		}

		public double BottomOffset
		{
			get
			{
				if (!(this.tbBottomOffset.Text == ""))
				{
					return Convert.ToDouble(this.tbBottomOffset.Text);
				}
				return 0.0;
			}
		}

		public double TopOffset
		{
			get
			{
				if (!(this.tbTopOffset.Text == ""))
				{
					return Convert.ToDouble(this.tbTopOffset.Text);
				}
				return 0.0;
			}
		}

		public double XOffset
		{
			get
			{
				if (!(this.textBoxXoff.Text == ""))
				{
					return Convert.ToDouble(this.textBoxXoff.Text);
				}
				return 0.0;
			}
		}

		public double YOffset
		{
			get
			{
				if (!(this.textBoxYoff.Text == ""))
				{
					return Convert.ToDouble(this.textBoxYoff.Text);
				}
				return 0.0;
			}
		}

		public double Angle
		{
			get
			{
				if (!(this.textBoxAngle.Text == ""))
				{
					return Convert.ToDouble(this.textBoxAngle.Text);
				}
				return 0.0;
			}
		}

		public ColumnInsert(UIApplication app)
		{
			this.InitializeComponent();
			this.pictureBox1 = new Previewer(1u);
			this.pictureBox1.Paint += this.pictureBox1_Paint;
			this.groupBox3.Controls.Add(this.pictureBox1);
			this.pictureBox1.Location = new System.Drawing.Point(5, 21);
			this.pictureBox1.Size = new Size(this.pbPreview.Width, this.pbPreview.Height);
			this.pictureBox1.TabStop = false;
			try
			{
				this.m_application = app;
				this.m_docment = app.ActiveUIDocument.Document;
				this.archDB = FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, this.m_application.Application.VersionNumber);
				this.initFamilyListByFamilyLib();
				this.initFamilyListByDoc();
				this.initFamilyList();
				this.InitializeLevelData();
				base.Icon = Resource.YArchitechIcon;
				this.ReadColumnData();
				this.imgW = this.pbPreview.Width;
				this.imgH = this.pbPreview.Height;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			graphics.Clear(System.Drawing.Color.White);
			this.RefreshPreview(graphics);
		}

		private void RefreshPreview(Graphics g)
		{
			try
			{
				if (this.InfoChanged)
				{
					this.InfoChanged = false;
					this.pictureBox1.Curves = this.legend.Curves;
					this.pictureBox1.ZoomExtents();
				}
			}
			catch (Exception)
			{
			}
		}

		private void initFamilyList()
		{
			this.tvwFamilyList.Nodes.Add(this.nodesFamilyLib);
		}

		private void initFamilyListByFamilyLib()
		{
			try
			{
				this.tvwFamilyList.Nodes.Clear();
				TreeNode treeNode = new TreeNode();
				treeNode.Text = "柱";
				TreeNode columnCategoryNode = this.GetColumnCategoryNode("建筑柱");
				TreeNode columnCategoryNode2 = this.GetColumnCategoryNode("结构柱");
				treeNode.Nodes.Add(columnCategoryNode);
				treeNode.Nodes.Add(columnCategoryNode2);
				this.nodesFamilyLib = treeNode;
			}
			catch
			{
			}
		}

		private TreeNode GetColumnCategoryNode(string categoryName)
		{
			TreeNode treeNode = new TreeNode();
			treeNode.Text = categoryName;
			foreach (DbFamily dbFamily in this.archDB.CategoryTable[categoryName].GetAllFamilies(false, CheckType.All))
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = dbFamily.Name;
				treeNode2.Tag = dbFamily;
				TreeNode treeNode3 = new TreeNode();
				treeNode3.Text = "Temp";
				treeNode2.Nodes.Add(treeNode3);
				treeNode.Nodes.Add(treeNode2);
			}
			return treeNode;
		}

		private void initFamilyListByDoc()
		{
			this.tvwFamilyList.Nodes.Clear();
			TreeNode treeNode = new TreeNode();
			treeNode.Text = "柱";
			TreeNode columnCategoryNode = this.GetColumnCategoryNode(BuiltInCategory.OST_Columns);
			TreeNode columnCategoryNode2 = this.GetColumnCategoryNode(BuiltInCategory.OST_StructuralColumns);
			treeNode.Nodes.Add(columnCategoryNode);
			treeNode.Nodes.Add(columnCategoryNode2);
			this.nodesDoc = treeNode;
		}

		private TreeNode GetColumnCategoryNode(BuiltInCategory category)
		{
			string text;
            if ((int)category == -2000100)
			{
				text = "建筑柱";
			}
			else
			{
				text = "结构柱";
			}
			TreeNode treeNode = new TreeNode();
			treeNode.Text = text;
			List<Family> list = new List<Family>();
			foreach (Element element in new FilteredElementCollector(this.m_docment).OfClass(typeof(FamilySymbol)).OfCategory(category).ToElements())
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (!list.Contains(familySymbol.Family, new FamilyIdComparer()))
				{
					list.Add(familySymbol.Family);
				}
			}
			foreach (Family family in list)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = family.Name;
				treeNode2.Tag = family;
				foreach (FamilySymbol familySymbol2 in (from ff in family.GetSymbols()
				orderby ff.Name
				select ff).ToList<FamilySymbol>())
				{
					TreeNode treeNode3 = new TreeNode();
					treeNode3.Text = familySymbol2.Name;
					treeNode3.Tag = familySymbol2;
					treeNode2.Nodes.Add(treeNode3);
				}
				treeNode.Nodes.Add(treeNode2);
			}
			return treeNode;
		}

		private void InitializeLevelData()
		{
			this.cmbTopLevel.Items.Clear();
			Level currentViewLevel = AssistFunc.GetCurrentViewLevel(this.m_docment);
			List<Level> sortLevels = Common.GetSortLevels(this.m_docment, double.MinValue, double.MaxValue);
			foreach (Level level in sortLevels)
			{
				if (currentViewLevel != null)
				{
					if (Geometry.Lessthan_Or_Equal(currentViewLevel.Elevation, level.Elevation) || sortLevels.Count == 1)
					{
						this.cmbTopLevel.Items.Add(level.Name);
					}
				}
				else
				{
					this.cmbTopLevel.Items.Add(level.Name);
				}
			}
			if (this.cmbTopLevel.Items.Count == 0)
			{
				return;
			}
			if (this.cmbTopLevel.Items.Count > 1)
			{
				this.cmbTopLevel.SelectedIndex = 1;
				return;
			}
			this.cmbTopLevel.SelectedIndex = 0;
		}

		private void tsbPick_Click(object sender, EventArgs e)
		{
			this.m_InsertType = 0;
			this.m_PickButton = true;
			this.tsbPick.Checked = true;
			this.tsbAxis.Checked = false;
			this.tsbRect.Checked = false;
			this.Layout();
		}

		private void tsbAxis_Click(object sender, EventArgs e)
		{
			this.m_InsertType = 1;
			this.m_PickButton = true;
			this.tsbPick.Checked = false;
			this.tsbAxis.Checked = true;
			this.tsbRect.Checked = false;
			this.Layout();
		}

		private void tsbRect_Click(object sender, EventArgs e)
		{
			this.m_InsertType = 2;
			this.m_PickButton = true;
			this.tsbPick.Checked = false;
			this.tsbAxis.Checked = false;
			this.tsbRect.Checked = true;
			this.Layout();
		}

		private void rdbAutoRotate_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbAutoRotate.Checked)
			{
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (!this.rdbAutoRotateWall.Checked && !this.rdbAutoRotate.Checked)
			{
				this.textBoxAngle.Enabled = true;
			}
		}

		private void rdoFamilyLib_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoFamilyLib.Checked)
			{
				this.tvwFamilyList.Nodes.Clear();
				this.tvwFamilyList.Nodes.Add(this.nodesFamilyLib);
				this.tvwFamilyList.Nodes[0].Expand();
				this.tvwFamilyList.Nodes[0].FirstNode.Expand();
				this.tvwFamilyList.Nodes[0].LastNode.Expand();
				if (this.tvwFamilyList.SelectedNode == null && this.nodeFamilyLib != null)
				{
					this.tvwFamilyList.SelectedNode = this.nodeFamilyLib;
					this.tvwFamilyList.Refresh();
					this.ModifyNodeColor();
				}
			}
		}

		private void rdoCurrentDoc_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoCurrentDoc.Checked)
			{
				this.tvwFamilyList.Nodes.Clear();
				this.tvwFamilyList.Nodes.Add(this.nodesDoc);
				this.tvwFamilyList.Nodes[0].Expand();
				this.tvwFamilyList.Nodes[0].FirstNode.Expand();
				this.tvwFamilyList.Nodes[0].LastNode.Expand();
				if (this.tvwFamilyList.SelectedNode == null && this.nodeDoc != null)
				{
					this.tvwFamilyList.SelectedNode = this.nodeDoc;
					this.tvwFamilyList.Refresh();
					this.ModifyNodeColor();
				}
			}
		}

		private void tvwFamilyList_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.GetNodeImage(e);
		}

		private void GetNodeImage(TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			bool flag;
			if (this.rdoFamilyLib.Checked)
			{
				this.nodeFamilyLib = node;
				flag = true;
			}
			else
			{
				this.nodeDoc = node;
				flag = false;
			}
			if (node.Level == 3)
			{
				bool flag2 = false;
				if (this.rdoFamilyLib.Checked && node.Parent.Text.Contains("矩形"))
				{
					DbFamily family = node.Tag as DbFamily;
					string text = node.Text;
					List<DbParameter> @params = this.archDB.TypeTable[family, text].Params;
					int num = 0;
					foreach (DbParameter dbParameter in @params)
					{
						string name = dbParameter.ParamDefinition.Name;
						if (name == "宽度")
						{
							num++;
							this.width = Convert.ToDouble(dbParameter.Value.ToString());
						}
						else if (name == "深度")
						{
							num += 2;
							this.depth = Convert.ToDouble(dbParameter.Value.ToString());
						}
						else if (name == "Width")
						{
							num++;
							this.width = Convert.ToDouble(dbParameter.Value.ToString());
						}
						else if (name == "Depth")
						{
							num += 2;
							this.depth = Convert.ToDouble(dbParameter.Value.ToString());
						}
					}
					if (num == 3)
					{
						this.pictureBox1.Enabled = false;
						this.pbPreview.Enabled = true;
						this.pbPreview.BringToFront();
						this.pbPreview.Image = this.imageList1.Images[this.index];
						this.pbPreview.Size = new Size(this.imgW, this.imgH);
						this.btnSel.Visible = true;
						string fullName = this.archDB.FamilyFileTable[family].FullName;
						Document doc = this.Get3DPreviewPicture(fullName);
						this.Update3DView(doc);
						flag2 = true;
					}
				}
				else
				{
					this.index = 0;
				}
				if (!flag2)
				{
					this.pbPreview.Size = new Size(this.imgW, this.imgH);
					this.btnSel.Visible = false;
					if (node.Tag is DbFamily)
					{
						DbFamily dbFamily = (DbFamily)node.Tag;
						string fullName2 = this.archDB.FamilyFileTable[dbFamily].FullName;
						Document doc2 = this.Get3DPreviewPicture(fullName2);
						this.Update3DView(doc2);
						this.Refresh2DImg(doc2, dbFamily);
						return;
					}
					if (node.Tag is FamilySymbol)
					{
						FamilySymbol familySymbol = (FamilySymbol)node.Tag;
						Document doc3 = this.m_docment.EditFamily(familySymbol.Family);
						this.Update3DView(doc3);
						this.Refresh2DImg(doc3, familySymbol);
						return;
					}
				}
			}
			else if (node.Level == 2)
			{
				if (flag)
				{
					this.AddFamilySymbol(node);
				}
				this.pbPreview.Size = new Size(this.imgW, this.imgH);
				this.btnSel.Visible = false;
				if (node.FirstNode.Tag is DbFamily)
				{
					DbFamily dbFamily2 = (DbFamily)node.FirstNode.Tag;
					string fullName3 = this.archDB.FamilyFileTable[dbFamily2].FullName;
					Document doc4 = this.Get3DPreviewPicture(fullName3);
					this.Update3DView(doc4);
					this.Refresh2DImg(doc4, dbFamily2);
				}
				else if (node.FirstNode.Tag is FamilySymbol)
				{
					FamilySymbol familySymbol2 = (FamilySymbol)node.FirstNode.Tag;
					Document doc5 = this.m_docment.EditFamily(familySymbol2.Family);
					this.Update3DView(doc5);
					this.Refresh2DImg(doc5, familySymbol2);
				}
				this.GetSelectedColumnType();
				if (this.m_nColType == 3)
				{
					this.cmbTopLevel.Enabled = false;
				}
			}
		}

		private void Refresh2DImg(Autodesk.Revit.DB.Document doc, DbFamily dbFamily)
		{
			this.legend = LegendCommon.GetFamily2DImg(this.m_application, doc, LegendType.Column);
			if (this.legend != null)
			{
				this.pictureBox1.Enabled = true;
				this.pbPreview.Enabled = false;
				this.InfoChanged = true;
				this.pictureBox1.BringToFront();
				this.pictureBox1.Refresh();
				return;
			}
			this.pictureBox1.Enabled = false;
			this.pbPreview.Enabled = true;
			this.pbPreview.BringToFront();
			this.pbPreview.Image = AssistFunc.ResizeImage((Bitmap)dbFamily.Image, this.imgW, this.imgH);
		}

		private void Refresh2DImg(Autodesk.Revit.DB.Document doc, FamilySymbol fs)
		{
			this.legend = LegendCommon.GetFamily2DImg(this.m_application, doc, LegendType.Column);
			if (this.legend != null)
			{
				this.pictureBox1.Enabled = true;
				this.pbPreview.Enabled = false;
				this.InfoChanged = true;
				this.pictureBox1.BringToFront();
				this.pictureBox1.Refresh();
				return;
			}
			this.pictureBox1.Enabled = false;
			this.pbPreview.Enabled = true;
			this.pbPreview.BringToFront();
			this.pbPreview.Image = fs.GetPreviewImage(new Size(this.imgW, this.imgH));
		}

		private void AddFamilySymbol(TreeNode node)
		{
			if (node.Nodes.Count == 1 && node.Nodes[0].Text == "Temp")
			{
				node.Nodes.Clear();
				DbFamily dbFamily = node.Tag as DbFamily;
				List<string> symbols = dbFamily.Symbols;
				symbols.Sort();
				if (symbols.Count > 0)
				{
					for (int i = 0; i < symbols.Count; i++)
					{
						TreeNode treeNode = new TreeNode();
						treeNode.Text = symbols[i];
						treeNode.Tag = dbFamily;
						node.Nodes.Add(treeNode);
					}
					this.tvwFamilyList.Refresh();
				}
			}
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			if (this.index == this.imageList1.Images.Count - 1)
			{
				this.index = -1;
			}
			PictureBox pictureBox = this.pbPreview;
			ImageList.ImageCollection images = this.imageList1.Images;
			int num = this.index + 1;
			this.index = num;
			pictureBox.Image = images[num];
		}

		private void GetSelectedColumnType()
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (this.rdoFamilyLib.Checked)
			{
				DbFamily dbFamily = selectedNode.Tag as DbFamily;
				if (dbFamily == null)
				{
					return;
				}
				if (this.archDB == null)
				{
					return;
				}
				string text = selectedNode.FirstNode.Text;
				DbFamilySymbol dbFamilySymbol = this.archDB.TypeTable[dbFamily, text];
				if (dbFamilySymbol == null)
				{
					return;
				}
				using (List<DbParameter>.Enumerator enumerator = dbFamilySymbol.Params.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbParameter dbParameter = enumerator.Current;
						string name = dbParameter.ParamDefinition.Name;
						if (name == "宽度" || name == "深度" || name == "Width" || name == "Depth")
						{
							this.m_nColType = 1;
						}
						if (name == "直径" || name == "Diameter")
						{
							this.m_nColType = 2;
						}
						if (name.Contains("高度"))
						{
							this.m_nColType = 3;
						}
					}
					return;
				}
			}
			foreach (object obj in (selectedNode.FirstNode.Tag as FamilySymbol).Parameters)
			{
				string name2 = ((Parameter)obj).Definition.Name;
				if (name2 == "宽度" || name2 == "深度" || name2 == "Width" || name2 == "Depth")
				{
					this.m_nColType = 1;
				}
				if (name2 == "直径" || name2 == "Diameter")
				{
					this.m_nColType = 2;
				}
				if (name2.Contains("高度"))
				{
					this.m_nColType = 3;
				}
			}
		}

		public FamilySymbol GetColumnFamilySymbol()
		{
			if (this.archDB == null)
			{
				return null;
			}
			FamilySymbol result;
			try
			{
				if (this.rdoFamilyLib.Checked)
				{
					TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
					DbFamily family = selectedNode.Tag as DbFamily;
					string text = selectedNode.Text;
					result = this.archDB.TypeTable[family, text].Create1(this.m_docment);
				}
				else
				{
					result = (this.tvwFamilyList.SelectedNode.Tag as FamilySymbol);
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private void tvwFamilyList_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (selectedNode != null)
			{
				selectedNode.BackColor = System.Drawing.Color.Empty;
				selectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void tvwFamilyList_Leave(object sender, EventArgs e)
		{
			this.ModifyNodeColor();
		}

		private void ModifyNodeColor()
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (selectedNode != null)
			{
				selectedNode.BackColor = System.Drawing.Color.RoyalBlue;
				selectedNode.ForeColor = System.Drawing.Color.White;
			}
		}

		private bool GetParameterData(ref List<ParameterData> parameterDataList)
		{
			DbFamily dbFamily = this.tvwFamilyList.SelectedNode.Tag as DbFamily;
			if (dbFamily == null)
			{
				return false;
			}
			if (this.archDB == null)
			{
				return false;
			}
			string text = this.tvwFamilyList.SelectedNode.FirstNode.Text;
			DbFamilySymbol dbFamilySymbol = this.archDB.TypeTable[dbFamily, text];
			if (dbFamilySymbol == null)
			{
				return false;
			}
			foreach (DbParameter dbParameter in dbFamilySymbol.Params)
			{
				double parValue = 0.0;
				try
				{
					parValue = AssistFunc.mmToFeet(Convert.ToDouble(dbParameter.Value.ToString()));
				}
				catch (Exception)
				{
					continue;
				}
				ParameterData item = new ParameterData(dbParameter.ParamDefinition.Name, parValue);
				parameterDataList.Add(item);
			}
			return true;
		}

		private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			string text = selectedNode.FirstNode.Text;
			if (this.rdoFamilyLib.Checked)
			{
				DbFamily dbFamily = selectedNode.Tag as DbFamily;
				List<string> symbols = dbFamily.Symbols;
				List<ParameterData> list = new List<ParameterData>();
				if (!this.GetParameterData(ref list))
				{
					return;
				}
				if (list.Count == 0)
				{
					MessageBox.Show("无有效尺寸参数（缺少尺寸参数或者参数分组错误）", "盈建科提示");
					return;
				}
				using (DlgNewSize dlgNewSize = new DlgNewSize(text, symbols, list))
				{
					if (dlgNewSize.ShowDialog() == DialogResult.OK)
					{
						string newTypeName = dlgNewSize.NewTypeName;
						DataTable dataTable = dbFamily.Database.TypeTable.DataTable(dbFamily);
						dataTable.Clear();
						DataRow dataRow = dataTable.NewRow();
						dataRow["规格名"] = newTypeName;
						foreach (ParameterData parameterData in list)
						{
							dataRow[parameterData.ParName] = AssistFunc.feetToMM(parameterData.ParValue);
						}
						dataTable.Rows.Add(dataRow);
						dbFamily.AddType(dataTable);
						TreeNode treeNode = new TreeNode();
						treeNode.Text = newTypeName;
						treeNode.Tag = dbFamily;
						selectedNode.Nodes.Add(treeNode);
						this.tvwFamilyList.SelectedNode = treeNode;
					}
				}
			}
		}

		private void tvwFamilyList_MouseDown(object sender, MouseEventArgs e)
		{
			if (this.rdoCurrentDoc.Checked)
			{
				return;
			}
			if (e.Button == MouseButtons.Right)
			{
				System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
				TreeNode nodeAt = this.tvwFamilyList.GetNodeAt(pt);
				if (nodeAt != null)
				{
					if (nodeAt.Level == 2)
					{
						nodeAt.ContextMenuStrip = this.cmsNewMenu;
					}
					if (nodeAt.Level != 0 || nodeAt.Level != 1)
					{
						this.tvwFamilyList.SelectedNode = nodeAt;
					}
				}
			}
		}

		private void SaveColumnData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "InsertColumn.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.rdoFamilyLib.Checked);
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (selectedNode != null)
			{
				if (selectedNode.Level == 0)
				{
					streamWriter.WriteLine(selectedNode.Text);
					streamWriter.WriteLine("");
					streamWriter.WriteLine("");
					streamWriter.WriteLine("");
				}
				else if (selectedNode.Level == 1)
				{
					streamWriter.WriteLine(selectedNode.Parent.Text);
					streamWriter.WriteLine(selectedNode.Text);
					streamWriter.WriteLine("");
					streamWriter.WriteLine("");
				}
				else if (selectedNode.Level == 2)
				{
					streamWriter.WriteLine(selectedNode.Parent.Parent.Text);
					streamWriter.WriteLine(selectedNode.Parent.Text);
					streamWriter.WriteLine(selectedNode.Text);
					streamWriter.WriteLine("");
				}
				else if (selectedNode.Level == 3)
				{
					streamWriter.WriteLine(selectedNode.Parent.Parent.Parent.Text);
					streamWriter.WriteLine(selectedNode.Parent.Parent.Text);
					streamWriter.WriteLine(selectedNode.Parent.Text);
					streamWriter.WriteLine(selectedNode.Text);
				}
			}
			else
			{
				streamWriter.WriteLine("");
				streamWriter.WriteLine("");
				streamWriter.WriteLine("");
				streamWriter.WriteLine("");
			}
			streamWriter.WriteLine(this.m_InsertType.ToString());
			streamWriter.WriteLine(this.tbBottomOffset.Text);
			streamWriter.WriteLine(this.tbTopOffset.Text);
			streamWriter.WriteLine(this.textBoxXoff.Text);
			streamWriter.WriteLine(this.textBoxYoff.Text);
			streamWriter.WriteLine(this.textBoxAngle.Text);
			streamWriter.WriteLine(this.rdbAutoRotate.Checked.ToString());
			streamWriter.WriteLine(this.rdbAutoRotateWall.Checked.ToString());
			streamWriter.WriteLine(this.rdbCustom.Checked.ToString());
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadColumnData()
		{
			string path = Path.GetTempPath() + "InsertColumn.dat";
			if (!File.Exists(path))
			{
				this.rdbCustom.Checked = true;
				this.SetDefaultItem();
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			bool flag = Convert.ToBoolean(streamReader.ReadLine());
			TreeNode treeNode;
			if (flag)
			{
				this.rdoFamilyLib.Checked = true;
				treeNode = this.nodesFamilyLib;
			}
			else
			{
				this.rdoCurrentDoc.Checked = true;
				treeNode = this.nodesDoc;
			}
			string value = streamReader.ReadLine();
			string text = streamReader.ReadLine();
			string text2 = streamReader.ReadLine();
			string text3 = streamReader.ReadLine();
			if (treeNode != null)
			{
				TreeNode treeNode2 = null;
				if (!string.IsNullOrEmpty(value))
				{
					treeNode2 = this.tvwFamilyList.Nodes[0];
				}
				if (treeNode2 != null && !string.IsNullOrEmpty(text) && this.ReadTempData(text, ref treeNode2) && !string.IsNullOrEmpty(text2) && this.ReadTempData(text2, ref treeNode2) && !string.IsNullOrEmpty(text3))
				{
					if (flag)
					{
						this.AddFamilySymbol(treeNode2);
					}
					this.ReadTempData(text3, ref treeNode2);
				}
				if (treeNode2 != null)
				{
					this.tvwFamilyList.SelectedNode = treeNode2;
					this.tvwFamilyList.Refresh();
					this.ModifyNodeColor();
				}
			}
			if (this.tvwFamilyList.SelectedNode == null)
			{
				this.SetDefaultItem();
			}
			string text4 = streamReader.ReadLine();
			this.m_InsertType = Convert.ToInt32(text4);
			if (this.m_InsertType == 0)
			{
				this.tsbPick.Checked = true;
				this.tsbAxis.Checked = false;
				this.tsbRect.Checked = false;
			}
			else if (this.m_InsertType == 1)
			{
				this.tsbPick.Checked = false;
				this.tsbAxis.Checked = true;
				this.tsbRect.Checked = false;
			}
			else if (this.m_InsertType == 2)
			{
				this.tsbPick.Checked = false;
				this.tsbAxis.Checked = false;
				this.tsbRect.Checked = true;
			}
			try
			{
				text4 = streamReader.ReadLine();
				if (text4.CompareTo("") != 0)
				{
					this.tbBottomOffset.Text = text4;
				}
				text4 = streamReader.ReadLine();
				if (text4.CompareTo("") != 0)
				{
					this.tbTopOffset.Text = text4;
				}
			}
			catch (Exception)
			{
				this.tbBottomOffset.Text = "0";
				this.tbTopOffset.Text = "0";
			}
			text4 = streamReader.ReadLine();
			this.textBoxXoff.Text = text4;
			text4 = streamReader.ReadLine();
			this.textBoxYoff.Text = text4;
			text4 = streamReader.ReadLine();
			this.textBoxAngle.Text = text4;
			text4 = streamReader.ReadLine();
			this.rdbAutoRotate.Checked = Convert.ToBoolean(text4);
			text4 = streamReader.ReadLine();
			this.rdbAutoRotateWall.Checked = Convert.ToBoolean(text4);
			try
			{
				text4 = streamReader.ReadLine();
				this.rdbCustom.Checked = Convert.ToBoolean(text4);
			}
			catch (Exception)
			{
				this.rdbCustom.Checked = true;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private bool ReadTempData(string nodeText, ref TreeNode node)
		{
			bool result = false;
			TreeNode treeNode = null;
			foreach (object obj in node.Nodes)
			{
				TreeNode treeNode2 = (TreeNode)obj;
				if (treeNode2.Text == nodeText)
				{
					treeNode = treeNode2;
					break;
				}
			}
			if (treeNode != null)
			{
				result = true;
				node = treeNode;
			}
			return result;
		}

		private void SetDefaultItem()
		{
			TreeNode treeNode = null;
			try
			{
				TreeNode treeNode2 = this.tvwFamilyList.Nodes[0].Nodes[0].Nodes[0];
				this.AddFamilySymbol(treeNode2);
				treeNode = treeNode2.FirstNode;
			}
			catch (Exception)
			{
				treeNode = this.tvwFamilyList.Nodes[0];
			}
			if (treeNode != null)
			{
				this.tvwFamilyList.SelectedNode = treeNode;
				this.tvwFamilyList.Refresh();
				this.ModifyNodeColor();
			}
		}

		private void rdbAutoRotateWall_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbAutoRotateWall.Checked || this.rdbAutoRotate.Checked)
			{
				this.textBoxAngle.Enabled = false;
				return;
			}
			this.textBoxAngle.Enabled = true;
		}

		private void tvwFamilyList_AfterExpand(object sender, TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			bool @checked = this.rdoFamilyLib.Checked;
			if (@checked && node.Level == 2)
			{
				this.AddFamilySymbol(node);
			}
		}

		private Document Get3DPreviewPicture(string path)
		{
			Autodesk.Revit.DB.Document document = null;
			try
			{
				document = this.m_application.Application.OpenDocumentFile(path);
				this.m_GeneralDocumentList.Add(document);
			}
			catch (Exception)
			{
				MessageBox.Show("文档打开出错", "盈建科提示");
			}
			return document;
		}

		private void Update3DView(Autodesk.Revit.DB.Document doc)
		{
			PreviewControl previewControl = this.elementHost.Child as PreviewControl;
			if (doc == null)
			{
				if (previewControl != null)
				{
					previewControl.Dispose();
					previewControl = null;
				}
				return;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View));
			IEnumerable<Autodesk.Revit.DB.View> enumerable = from Element f in filteredElementCollector
			where (f as Autodesk.Revit.DB.View).CanBePrinted
			select f as Autodesk.Revit.DB.View;
			Autodesk.Revit.DB.View view = null;
			foreach (Autodesk.Revit.DB.View view2 in enumerable)
			{
				if ((int)view2.ViewType == 4)
				{
					view = view2;
					break;
				}
			}
			if (view == null)
			{
				foreach (Autodesk.Revit.DB.View view3 in enumerable)
				{
					if ((int)view3.ViewType == 1)
					{
						view = view3;
						break;
					}
				}
			}
			if (view == null)
			{
				if (previewControl != null)
				{
					previewControl.Dispose();
					previewControl = null;
				}
				return;
			}
			if (previewControl != null)
			{
				previewControl.Dispose();
			}
			this.elementHost.Child = new PreviewControl(doc, view.Id);
		}

		public void CloseDocuments()
		{
			foreach (Autodesk.Revit.DB.Document document in this.m_GeneralDocumentList)
			{
				try
				{
					document.Close(false);
				}
				catch (Exception)
				{
				}
			}
		}

		private new void Layout()
		{
			double num;
			if (!double.TryParse(this.tbTopOffset.Text, out num))
			{
				MessageBox.Show("顶部偏移输入不合法，请重新输入。", "盈建科提示");
				this.tbTopOffset.Text = "0";
				this.tbTopOffset.Focus();
				return;
			}
			if (!Common.IsValidating(this.tbTopOffset.Text, -1000.0, 1000.0))
			{
				MessageBox.Show("顶部偏移的合法范围在-1000～1000之间。", "盈建科提示");
				this.tbTopOffset.Text = "0";
				this.tbTopOffset.Focus();
				return;
			}
			if (!double.TryParse(this.tbBottomOffset.Text, out num))
			{
				MessageBox.Show("底部偏移输入不合法，请重新输入。", "盈建科提示");
				this.tbBottomOffset.Text = "0";
				this.tbBottomOffset.Focus();
				return;
			}
			if (!Common.IsValidating(this.tbBottomOffset.Text, -1000.0, 1000.0))
			{
				MessageBox.Show("底部偏移的合法范围在-1000～1000之间。", "盈建科提示");
				this.tbBottomOffset.Text = "0";
				this.tbBottomOffset.Focus();
				return;
			}
			if (!double.TryParse(this.textBoxXoff.Text, out num))
			{
				MessageBox.Show("偏心X输入不合法，请重新输入。", "盈建科提示");
				this.textBoxXoff.Text = "0";
				this.textBoxXoff.Focus();
				return;
			}
			if (!Common.IsValidating(this.textBoxXoff.Text, -1000.0, 1000.0))
			{
				MessageBox.Show("偏心X的合法范围在-1000～1000之间。", "盈建科提示");
				this.textBoxXoff.Text = "0";
				this.textBoxXoff.Focus();
				return;
			}
			if (!double.TryParse(this.textBoxYoff.Text, out num))
			{
				MessageBox.Show("偏心Y输入不合法，请重新输入。", "盈建科提示");
				this.textBoxYoff.Text = "0";
				this.textBoxYoff.Focus();
				return;
			}
			if (!Common.IsValidating(this.textBoxYoff.Text, -1000.0, 1000.0))
			{
				MessageBox.Show("偏心Y的合法范围在-1000～1000之间。", "盈建科提示");
				this.textBoxYoff.Text = "0";
				this.textBoxYoff.Focus();
				return;
			}
			if (!double.TryParse(this.textBoxAngle.Text, out num))
			{
				MessageBox.Show("自定义角度输入不合法，请重新输入。", "盈建科提示");
				this.textBoxAngle.Text = "0";
				this.textBoxAngle.Focus();
				return;
			}
			if (!Common.IsValidating(this.textBoxAngle.Text, 0.0, 360.0))
			{
				MessageBox.Show("自定义角度的合法范围在0～360之间。", "盈建科提示");
				this.textBoxAngle.Text = "0";
				this.textBoxAngle.Focus();
				return;
			}
			if (this.tvwFamilyList.SelectedNode == null || this.tvwFamilyList.SelectedNode.Level != 3)
			{
				MessageBox.Show("请选择族类型。", "盈建科提示");
				return;
			}
			if (this.tvwFamilyList.SelectedNode.Parent.Parent.Text == "结构柱")
			{
				this.m_IsArchColumn = false;
			}
			else
			{
				this.m_IsArchColumn = true;
			}
			this.SaveColumnData();
			base.DialogResult = DialogResult.OK;
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.DialogResult = DialogResult.Cancel;
			}
			return false;
		}

		private UIApplication m_application;

		private Document m_docment;

		private FamilyLibDatabase archDB;

		private int m_InsertType;

		private int m_nColType = 1;

		private bool m_PickButton;

		private bool m_IsArchColumn = true;

		private TreeNode nodesDoc;

		private TreeNode nodesFamilyLib;

		private TreeNode nodeDoc;

		private TreeNode nodeFamilyLib;

		public int index;

		public double width;

		public double depth;

		private List<Document> m_GeneralDocumentList = new List<Document>();

		private int imgW;

		private int imgH;

		private Previewer pictureBox1;

		private bool InfoChanged;

		private Legend legend;
	}
}
